package com.ng.desktop.view.receta;

import java.awt.Dimension;
import java.awt.Frame;
import java.util.Arrays;
import java.util.List;

import com.ng.desktop.model.Receta;
import com.ng.desktop.persistence.RecetaPersistence;
import com.ng.desktop.services.NGFactoryBean;
import com.ng.desktop.services.THListingService;
import com.ng.framework.persistence.NGSearchParameter.Type;
import com.ng.framework.persistence.NGSearchParameter.UnaryOperation;
import com.ng.framework.view.desktop.NGEntitySelectorDialog;
import com.ng.framework.view.desktop.NGEntitySelectorField;
import com.ng.framework.view.desktop.NGGUIUtils;
import com.ng.framework.view.desktop.NGTablePanel;
import com.ng.framework.view.desktop.filters.NGComboFilter;
import com.ng.framework.view.desktop.filters.NGFilter;
import com.ng.framework.view.desktop.filters.NGTextFieldFilter;

public class RecetaDialog extends NGEntitySelectorDialog<Receta, RecetaPersistence> {
	
	private static final long serialVersionUID = -870384850244806080L;

	private THListingService thListingService;
	
	public RecetaDialog(
			Frame owner,
			NGEntitySelectorField<Receta, RecetaPersistence> ngEntitySelectorField) {
		super(owner, ngEntitySelectorField);
		
		this.setIconImage(NGGUIUtils.getImageIcon("icons/cutlery.png").getImage());
		this.setTitle("Gestión de Recetas");
		this.setSize(new Dimension(600, 600));
		this.setLocationRelativeTo(owner);
	}

	@Override
	protected RecetaPersistence createPersistence() {
		return (RecetaPersistence) NGFactoryBean.getInstance().getBean("recetaPersistence");
	}

	@Override
	protected List<NGFilter> createFilters() {
		NGFilter filtroAlimento = new NGComboFilter("Alimento", Receta.class, "alimentos", Type.INTEGER, UnaryOperation.EQUALS, "id", this.getListingService().getList(THListingService.ALIMENTOS, null, null));
		NGFilter filtroNombre = new NGTextFieldFilter("Nombre", Receta.class, "nombre", Type.STRING, UnaryOperation.LIKE);
		
		return Arrays.asList(new NGFilter[] { filtroAlimento, filtroNombre });
	}
	
	private THListingService getListingService() {
		if(thListingService == null) {
			thListingService = (THListingService) NGFactoryBean
			.getInstance().getBean("thListingService");
		}
		return thListingService;
	}

	@Override
	protected NGTablePanel<Receta, RecetaPersistence> createTable(
			RecetaPersistence persistence,
			NGEntitySelectorField<Receta, RecetaPersistence> ngEntitySelectorField) {
		return new RecetaTablePanel(persistence, ngEntitySelectorField);
	}

}
